/**
 *
 * @param {string} s
 */
function romanToInt(s) {
    const map = {
        I: 1,
        V: 5,
        X: 10,
        L: 50,
        C: 100,
        D: 500,
        M: 1000,
        IV: 4,
        IX: 9,
        XL: 40,
        XC: 90,
        CD: 400,
        CM: 900,
    };

    let momoIndex;

    let oneJudge = ["I", "X", "C"];
    let twoJudge = {
        I: ["V", "X"],
        X: ["L", "C"],
        C: ["D", "M"],
    };

    return s
        .split("")
        .map((p, index, array) => {
            if (momoIndex === index) {
                momoIndex = null;
                return null;
            }
            if (
                oneJudge.includes(p) &&
                twoJudge[p].includes(array[index + 1])
            ) {
                momoIndex = index + 1;
                return map[`${p}${array[index + 1]}`];
            }

            return map[p];
        })
        .reduce((sum, currentValue) => {
            if (!currentValue) return sum;
            return sum + Number(currentValue);
        }, 0);
}

console.log(romanToInt("DCXXI"));
